package com.deneme.ozveri.adapters;

import java.text.DecimalFormat;
import java.util.ArrayList;


import com.deneme.ozveri.objects.MenuOrderItem;
import com.deneme.ozverimenu.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

import android.widget.LinearLayout;

import android.widget.TextView;

public class MenuOrderListAdapter extends BaseAdapter {

	private ArrayList<MenuOrderItem> orderList;
	private Context ctxt;

	public MenuOrderListAdapter(ArrayList<MenuOrderItem> list, Context context) {
		orderList = list;
		ctxt = context;
	}

	@Override
	public int getCount() {
		return orderList != null ? orderList.size() : 0;
	}

	@Override
	public Object getItem(int position) {

		return orderList != null ? orderList.get(position) : null;
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout itemLayout;
		LayoutInflater inflater = (LayoutInflater) ctxt
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (orderList == null || orderList.size() == 0) {
			return null;
		}

		MenuOrderItem currItem = orderList.get(position);

		itemLayout = (RelativeLayout) inflater.inflate(
				R.layout.menuorderitem_row, parent, false);

		TextView name = (TextView) itemLayout
				.findViewById(R.id.menuorederitem_row_order_name_txt);
		name.setText(currItem.getOrderName());

		TextView price = (TextView) itemLayout
				.findViewById(R.id.menuorederitem_row_order_price_txt);
		price.setText(""+roundTwoDecimals((double)currItem.getOrderPrice()/100));

		TextView shortDesc = (TextView) itemLayout
				.findViewById(R.id.menuorederitem_row_order_shortdesc_txt);
		shortDesc.setText(currItem.getOrderShortDescription());

		/* must add image view for ordersmallimage */
		/* TODO check best way to image init with cache */

		itemLayout.setBackgroundColor((position % 2) == 1 ? ctxt
				.getApplicationContext().getResources()
				.getColor(R.color.rowcolor0) : ctxt.getApplicationContext()
				.getResources().getColor(R.color.rowcolor1));

		return itemLayout;
	}
	
	
	double roundTwoDecimals(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.##");
    return Double.valueOf(twoDForm.format(d));
}

}
